package com.cynoware.busappmas.task;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.util.Log;

import com.cynoware.busappmas.model.Config;
import com.cynoware.busappmas.model.UsbUpdateModel;
import com.cynoware.busappmas.util.GeneralUtil;

public class UsbStatesReceiver extends BroadcastReceiver  {
	private Context execactivity;
	private ContentResolver mContentResolver;

	public static final int USB_STATE_MSG = 0x00020;  
    public static final int USB_STATE_ON = 0x00021;  
    public static final int USB_STATE_OFF = 0x00022;
	public static final String TAG = "UsbStatesReceiver";  
    public IntentFilter filter = new IntentFilter();
    private static UsbStatesReceiver usbreceiver=new UsbStatesReceiver();
    
    public static UsbStatesReceiver instance(){
    	return usbreceiver;
    }
    
	public void init(Context context,ContentResolver mContentResolver){
		this.mContentResolver=mContentResolver;
		execactivity = context;
		filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		filter.addAction(Intent.ACTION_MEDIA_EJECT);
		filter.addAction(Intent.ACTION_MEDIA_REMOVED);
		filter.addDataScheme("file"); 	
	}

	public Intent start() {
		return execactivity.registerReceiver(this, filter);
	}

	public void stop() {
		execactivity.unregisterReceiver(this);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		 
	    String action=intent.getAction();
	    Config mConfig=Config.instance(mContentResolver);
	    if(action.equals(Intent.ACTION_MEDIA_MOUNTED )){  
	       System.out.println("--------------------------on");
	       if(mConfig.getUsbInsert().getValue().equals("0")){
	    	   mConfig.setUsbInsert("1");
		       String   fileStr = "/storage/usbhost0/cfg";
		       String[] result  = null;
		       int      havecfg = 0;
		       File     file    = null;
		       StorageManager storageManager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
		       try {
		           Method method = StorageManager.class.getMethod("getVolumePaths");
		           method.setAccessible(true);
		           try {
		               result = (String[])method.invoke(storageManager);
		           } catch (InvocationTargetException e) {
		               e.printStackTrace();
		           }
		           for (int i = 0; i < result.length; i++) {
		               Log.d(TAG, "path----> " + result[i] + "\n");
		               if (result[i].indexOf("usb") != -1) {
		            	   fileStr = result[i] + "/udisk0/cfg";
		            	   Log.d(TAG, "fileStr = " + fileStr);
		            	   file = new File(fileStr);
		    		       if(file.exists()){
		    		    	   havecfg = 1;
		    		    	   break;
		    		       }
		               } 
		           }
		       } catch (Exception e) {
		           e.printStackTrace();
		       }
		       
		       if(havecfg == 1){
		    	   Log.d(TAG, "usb start");
			   	   UsbUpdateModel usbSync = UsbUpdateModel.instance();
			       usbSync.init(mContentResolver,execactivity);
			       usbSync.Start();
		       }
	       }
	    }else{  
	    	System.out.println("---------------------out");
	    	mConfig.setUsbInsert("0");
	    	GeneralUtil.Reboot();
	       
	    }  

	};
	
	
	
}
